﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Quality.BasicData;
using System.Data.SqlClient;

namespace Quality.BasicData
{
    public partial class testclass : Form
    {
        public testclass()
        {
            InitializeComponent();
            this.dataGridView1.AutoGenerateColumns = false;
        }

       
        private void testclass_Load(object sender, EventArgs e)
        {
        frm_TransfEvent();
        }
        private void 新建toolStripButton1_Click(object sender, EventArgs e)
        {
            string id = "";
            testclassedit frm = new testclassedit(id);

            frm.WindowState = FormWindowState.Normal;
            frm.Dock = DockStyle.Fill;
            frm.TransfEvent += frm_TransfEvent;
            frm.Show();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); ;
            testclassedit frm = new testclassedit(id);

            frm.WindowState = FormWindowState.Normal;
            frm.Dock = DockStyle.Fill;
            frm.TransfEvent += frm_TransfEvent;
            frm.Show();
        }
        //事件处理方法
        void frm_TransfEvent()
        {
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = new SqlCommand();
            if (keytoolStripTextBox.Text == "")
            {

                da.SelectCommand.CommandText = "select top 1000 id,num,classname from testclass  order by num";

            }
            else
            {

                da.SelectCommand.CommandText = "select id,num,classname from testclass  where num like '%@key%' or classname like '%@key%' order by num";
                da.SelectCommand.Parameters.AddWithValue("@key", keytoolStripTextBox.Text.Trim());

            }
            DataSet dt = DB.getlist(da);
            dataGridView1.DataSource = dt.Tables[0];

        }

        private void 查询toolStripButton1_Click(object sender, EventArgs e)
        {
            frm_TransfEvent();
        }

        private void 删除toolStripButton1_Click(object sender, EventArgs e)
        {
            int count = sqlHelper.Count("select * from testtypes  where classnum='" + dataGridView1.CurrentRow.Cells[1].Value.ToString() + "'");
            if (count > 0) { MessageBox.Show("检验分类资料已被使用，不能删除！"); return; }
            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("确定要删除吗?", "删除", messButton);
            if (dr == DialogResult.OK)
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {

                    SqlCommand comm = new SqlCommand();
                    comm.CommandText = "delete from testclass where id=@id";
                    comm.Parameters.AddWithValue("@id", dataGridView1.CurrentRow.Cells[0].Value.ToString());
                    DB.dosql(comm);
                    frm_TransfEvent();

                }
            }
 
            
        }

       

    }
}
